%%%-------------------------------------------------------------------
%%% @author Rain
%%% @doc 帧同步相关接口
%%% @end
%%%-------------------------------------------------------------------
-module(ls_util).
-author("Rain").
-include("elock_step.hrl").

%% API
-export([open_room/1]).
-export([create_ls_table/0, init_ls_uniq/0, new_match_id/0]).
-export([start_mnesia/0]).

-export([get_room_pid/1, get_match_id/1]).
-export([get_ls_role_match/1, set_ls_role_match/1, del_ls_role_match/1]).
-export([get_ls_match_room/1, set_ls_match_room/1, del_ls_match_room/1]).

-define(TABLE_DEF(Tab), [{type, set}, {ram_copies, [node()]}, {record_name, Tab}, {attributes, record_info(fields, Tab)}]).

start_mnesia() ->
    mnesia:start(),
    create_ls_table().

%% @doc 创建帧同步相关数据表
create_ls_table() ->
    {atomic, ok} = mnesia:create_table(?TABLE_LS_UNIQ_ID, ?TABLE_DEF(?TABLE_LS_UNIQ_ID)),
    {atomic, ok} = mnesia:create_table(?TABLE_LS_MATCH_ROOM, ?TABLE_DEF(?TABLE_LS_MATCH_ROOM)),
    {atomic, ok} = mnesia:create_table(?TABLE_LS_ROLE_MATCH, ?TABLE_DEF(?TABLE_LS_ROLE_MATCH)),
    init_ls_uniq(),
    ok.




%% @doc 初始化帧同步相关的唯一id
init_ls_uniq() ->
    %% 初始化比赛id
    mnesia:dirty_write(?TABLE_LS_UNIQ_ID, #ls_uniq_id{key = match_id}).

%% @doc 获取一个最新的比赛id作为新对局的id和房间标识
new_match_id() ->
    new_ls_uniq(match_id).

%% @doc 获取一个新的唯一id
new_ls_uniq(Key) ->
    case mnesia:dirty_update_counter(ls_uniq_id, Key, 1) of
        Value when is_integer(Value) ->
            {ok, Value};
        Any ->
            Any
    end.

%% @doc 开启房间
%% OpenParams: #{}
%% return: {ok, Pid} | Error
open_room(RoomParams) ->
    Sup = elock_step_sup,
    case catch supervisor:start_child(ls_room_sup, [RoomParams]) of
        {ok, Pid} when is_pid(Pid) ->
            {ok, Pid};
        {error, {already_started, Pid}} ->
            ?LS_WARN("ls_room ~p is already started!, sup:~p", [RoomParams, Sup]),
            {ok, Pid};
        Other ->
            ?LS_ERROR("OpenRoomFail ~p, Reason ~p", [RoomParams, Other]),
            throw(Other)
    end.

%% @doc 获取上次比赛的匹配id
get_match_id(RoleId) ->
    case get_ls_role_match(RoleId) of
        #ls_role_match{match_id = MatchId} ->
            MatchId;
        _ ->
            0
    end.

%% @doc 获取角色匹配数据
get_ls_role_match(RoleId) ->
    case ets:lookup(?TABLE_LS_ROLE_MATCH, RoleId) of
        [#ls_role_match{} = LsRoleMatch] ->
            LsRoleMatch;
        _ ->
            undefined
    end.

%% @doc 设置角色匹配数据
set_ls_role_match(LsRoleMatch = #ls_role_match{}) ->
    ets:insert(?TABLE_LS_ROLE_MATCH, LsRoleMatch).

%% @doc 删除角色匹配数据
del_ls_role_match(RoleId) ->
    ets:delete(?TABLE_LS_ROLE_MATCH, RoleId).

%% @doc 获取匹配房间数据
get_ls_match_room(MatchId) ->
    case ets:lookup(?TABLE_LS_MATCH_ROOM, MatchId) of
        [#ls_match_room{} = LsMatchRoom] ->
            LsMatchRoom;
        _ ->
            undefined
    end.

%% @doc 设置匹配房间数据
set_ls_match_room(LsMatchRoom = #ls_match_room{}) ->
    ets:insert(?TABLE_LS_MATCH_ROOM, LsMatchRoom).

%% @doc 删除匹配房间数据
del_ls_match_room(MatchId) ->
    ets:delete(?TABLE_LS_MATCH_ROOM, MatchId).

%% @doc 根据比赛id获取房间的pid
%% pid() | undefined
get_room_pid(MatchId) ->
    case get_ls_match_room(MatchId) of
        #ls_match_room{room_pid = RoomPid} ->
            RoomPid;
        _ ->
            undefined
    end.







